/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import cn.hutool.core.map.MapUtil;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.constant.ConstantUtils;
import me.zhengjie.modules.system.domain.CourseInfo;
import me.zhengjie.modules.system.domain.CourseInfoRequest;
import me.zhengjie.modules.system.service.CourseInfoClockSourceService;
import me.zhengjie.modules.system.service.CourseInfoService;
import me.zhengjie.modules.system.service.CourseItemService;
import me.zhengjie.modules.system.service.dto.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author 王宇
* @date 2023-09-26
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "课程信息管理")
@RequestMapping("/api/courseInfo")
public class CourseInfoController {

    private final CourseInfoService courseInfoService;

    @Autowired
    private CourseItemService courseItemService;

    @Autowired
    private CourseInfoClockSourceService courseInfoClockSourceService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('courseInfo:list')")
    public void download(HttpServletResponse response, CourseInfoQueryCriteria criteria) throws IOException {
        courseInfoService.download(courseInfoService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询课程信息")
    @ApiOperation("查询课程信息")
    @PreAuthorize("@el.check('courseInfo:list')")
    //@AnonymousAccess
    public ResponseEntity<Object> query(CourseInfoQueryCriteria criteria, Pageable pageable){
        Map<String, Object> map = courseInfoService.queryAll(criteria, pageable);
        if (MapUtil.isNotEmpty(map) && map.get("content") != null) {
            List<CourseInfoDto> contentList = (List<CourseInfoDto>) map.get("content");
            contentList.parallelStream().forEach(s -> {
                CourseItemQueryCriteria courseItemQueryCriteria = new CourseItemQueryCriteria();
                courseItemQueryCriteria.setCourseNo(s.getCourseNo());
                courseItemQueryCriteria.setItemStatus(ConstantUtils.Valid);
                List<CourseItemDto> courseItemDtos = courseItemService.queryAll(courseItemQueryCriteria);
                s.setItemList(courseItemDtos);

                CourseInfoClockSourceQueryCriteria queryCriteria = new CourseInfoClockSourceQueryCriteria();
                queryCriteria.setCourseNo(s.getCourseNo());
                List<CourseInfoClockSourceDto> sourceList = courseInfoClockSourceService.queryAll(queryCriteria);
                if (!CollectionUtils.isEmpty(sourceList)){
                    List<CourseInfoDto.CourseClassSourceDTO> courseClassSourceDTOS = new ArrayList<>();
                    for (CourseInfoClockSourceDto courseInfoClockSourceDto : sourceList) {
                        CourseInfoDto.CourseClassSourceDTO sourceDTO = new CourseInfoDto.CourseClassSourceDTO();
                        sourceDTO.setClassNo(courseInfoClockSourceDto.getClassNo());
                        sourceDTO.setClassName(courseInfoClockSourceDto.getClassName());
                        courseClassSourceDTOS.add(sourceDTO);
                    }
                    s.setClassList(courseClassSourceDTOS);
                }
            });
        }
        return new ResponseEntity<>(map,HttpStatus.OK);
    }

   /* @PostMapping
    @Log("新增课程信息")
    @ApiOperation("新增课程信息")
    @PreAuthorize("@el.check('courseInfo:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody CourseInfo resources){
        return new ResponseEntity<>(courseInfoService.create(resources),HttpStatus.CREATED);
    }*/
    @PostMapping
    @Log("新增课程信息")
    @ApiOperation("新增课程信息-自定义")
    @PreAuthorize("@el.check('courseInfo:add')")
    //@AnonymousAccess
    public ResponseEntity<Object> add(@Validated @RequestBody CourseInfoRequest resources){
        return new ResponseEntity<>(courseInfoService.add(resources),HttpStatus.CREATED);
    }

   /* @PutMapping
    @Log("修改课程信息")
    @ApiOperation("修改课程信息")
    @PreAuthorize("@el.check('courseInfo:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody CourseInfo resources){
        courseInfoService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }*/
    @PutMapping
    @Log("修改课程信息")
    @ApiOperation("修改课程信息-自定义")
    @PreAuthorize("@el.check('courseInfo:edit')")
    //@AnonymousAccess
    public ResponseEntity<Object> modify(@Validated @RequestBody CourseInfoRequest resources){
        courseInfoService.modify(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除课程信息")
    @ApiOperation("删除课程信息")
    @PreAuthorize("@el.check('courseInfo:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Long[] ids) {
        courseInfoService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}